package crudfx.field;
import crudfx.interop.*;
import crudfx.navigation.*;
import crudfx.util.*;
import crudfx.data.*;
import crudfx.util.resources.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class TextField extends UIComponent{
    protected var jTextArea:JTextArea;
    protected var jScrollPane:JScrollPane;
    var dropCatcher:DropCatcher;
    public var onKey:function():Void;
    public var keyEvent: KeyEvent;
    public var popup:PopUp=null;
    public var text:String="" on replace {
        jTextArea.setText(text);
        }
    public var onLostFocus:function():Void;
    //public var tooltip:String="TextField" on replace{jTextArea.setToolTipText(tooltip);};
    public function cut():Void{jTextArea.cut();text=jTextArea.getText();}
    public function copy():Void{jTextArea.copy();}
    public function paste():Void{jTextArea.paste();}
    public-init var onDrop:function(text:String[]):Void;
    public function selectAll():Void {
        jTextArea.setSelectionStart(0);
        jTextArea.setSelectionEnd(jTextArea.getText().length());
    }
    public var leftToRight:Boolean=true on replace{
        if(leftToRight)
            jTextArea.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT)
            else jTextArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT );
        };
    init{
        UIManager.put("TextArea.font"
                    ,UIManager.get("TextField.font"));
        jScrollPane=new JScrollPane();
        jTextArea=new JTextArea();
        jTextArea.setText(text);
        jScrollPane.getViewport().add(jTextArea);
        //jTextArea.setToolTipText(tooltip);
        var mouseAdapter:MouseAdapter=MouseAdapter{
            override public function mousePressed(e:MouseEvent):Void{
                if(e.isPopupTrigger()){
                    var relative:Point = e.getPoint();
                    showPopup(relative.x,relative.y); } }
            override public function mouseReleased(e:MouseEvent):Void{
                if(e.isPopupTrigger()){
                    var relative:Point = e.getPoint();
                    showPopup(relative.x,relative.y); } }
            }
        jTextArea.addMouseListener(mouseAdapter);
        var keyAdapter:KeyAdapter=KeyAdapter{
            override public function keyPressed(evt:KeyEvent):Void{
                keyEvent = evt;
                var ss:Integer=jTextArea.getSelectionStart();
                var se:Integer=jTextArea.getSelectionEnd();
                text=jTextArea.getText();
                jTextArea.setSelectionStart(ss);
                jTextArea.setSelectionEnd(se);
                }
            override public function keyReleased(evt:KeyEvent):Void{
                keyEvent = evt;
                var ss:Integer=jTextArea.getSelectionStart();
                var se:Integer=jTextArea.getSelectionEnd();
                text=jTextArea.getText();
                jTextArea.setSelectionStart(ss);
                jTextArea.setSelectionEnd(se);
                }
            override public function keyTyped(evt:KeyEvent):Void{
                keyEvent = evt;
                var ss:Integer=jTextArea.getSelectionStart();
                var se:Integer=jTextArea.getSelectionEnd();
                text=jTextArea.getText();
                jTextArea.setSelectionStart(ss);
                jTextArea.setSelectionEnd(se);
                onKey();
                }
            };
        jTextArea.addKeyListener(keyAdapter);
        if(leftToRight)
            jTextArea.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT)
            else jTextArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT );
        var focusAdapter:FocusAdapter=FocusAdapter{
            override public function focusGained(evt:FocusEvent):Void { }
            override public function focusLost(evt:FocusEvent):Void {
                text=jTextArea.getText();
                onLostFocus();
               }
            };
        jTextArea.addFocusListener(focusAdapter);
        if(onDrop==null){
            onDrop=function(t:String[]):Void{
                var s:String="";
                var f:Boolean=true;
                for(i:String in t){
                    if(not f)s="{s}\n";
                    f=false;
                    s="{s}{i}";
                    }
                var n=jTextArea.getSelectionStart();

                text="{text.substring(0,n)}{s}{text.substring(n)}";
                };
            }
        dropCatcher=DropCatcher{
            component:jTextArea
            onDrop:onDrop
            };
        jTextArea.setDragEnabled(true);
        jTextArea.setSelectionStart(0);
        jTextArea.setSelectionEnd(0);
        }
    function showPopup(x:Integer,y:Integer):Void{
        if(popup==null){
            popup=PopUp{
                items:[
                    MenuLink{
                        text:"Copy"
                        action:copy
                        icon:ResourceLoader.imageIcon("copy.png")
                        }
                    ,MenuLink{
                        text:"Cut"
                        action:cut
                        icon:ResourceLoader.imageIcon("cut.png")
                        }
                    ,MenuPad{}
                    ,MenuLink{
                        text:"Paste"
                        action:paste
                        icon:ResourceLoader.imageIcon("paste.png")
                        }
                    ,MenuPad{}
                    ,MenuLink{ text:"Select all" action:selectAll }
                    ]
                }
            }
        popup.show(this,x,y);
    }
    override public function getJComponent():JComponent{
        return jScrollPane;
        }
    override public function highlight(s:String):Boolean{
        return false;
        }
    override public function gather():DataTree{
        var me:DataTree=DataTree{ name:"TextField" };
        me.node("text").asString("").value=text;
        return me;
        }
    }
